package com.shenhaofeng.app.device;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.net.ConnectivityManagerCompat;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;


/**
 * 设备连接状态的订阅源,可以注册设备连接的改变事件
 * Created by Administrator on 2016/10/11.
 */
public class ConnectChangeSource {


    private BroadcastReceiver mBroadcastReceiver;

    private Context mContext;

    private List<NetworkInfoListener> mNetworkInfoListeners = new ArrayList<>();


    /**
     * 创建一个实例
     *
     * @param context
     * @return
     */
    public static ConnectChangeSource createInstance(Context context) {
        return new ConnectChangeSource(context);
    }


    ConnectChangeSource(Context context) {
        this.mContext = context;
        mBroadcastReceiver = new ConnectChangeReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(mBroadcastReceiver, intentFilter);
    }


    /**
     * 在组件被销毁的时候请调用该方法，否则资源将不能被释放
     */
    public void onDestroy() {
        if (mBroadcastReceiver != null) {
            mContext.unregisterReceiver(mBroadcastReceiver);
        }
    }


    /**
     * 注册一个监听器,在网络连接发生改变的时候将会通知
     *
     * @param networkInfoListener 监听器
     */
    public void registerNetworkInfoListener(NetworkInfoListener networkInfoListener) {
        if (mNetworkInfoListeners.contains(networkInfoListener)) {
            throw new IllegalArgumentException("networkInfoListener already existed");
        }
        mNetworkInfoListeners.add(networkInfoListener);
    }


    /**
     * 注销网络状态变化的监听器
     *
     * @param networkInfoListener 监听器
     */
    public void unregisterNetworkInfoListener(NetworkInfoListener networkInfoListener) {
        if (!mNetworkInfoListeners.contains(networkInfoListener)) {
            throw new IllegalArgumentException("networkInfoListener not existed");
        }
        mNetworkInfoListeners.remove(networkInfoListener);
    }


    class ConnectChangeReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = ConnectivityManagerCompat.getNetworkInfoFromBroadcast(connectivityManager, intent);
            for (int i = 0; i < mNetworkInfoListeners.size(); i++) {
                mNetworkInfoListeners.get(i).onNewNetWorkInfo(networkInfo);
            }

        }
    }



    /**
     * 网络信息监听器
     */
    public interface NetworkInfoListener {
        /**
         * 在网络状态改变时将会被调用
         *
         * @param networkInfo 参数有可能为null,但是网络确实改变了
         */
        void onNewNetWorkInfo(@Nullable NetworkInfo networkInfo);
    }
}
